package com.springapi.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.springapi.entity.Goodsmanager;
import com.springapi.service.GoodsmanagerService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Goodsmanager)表控制层
 *
 * @author makejava
 * @since 2024-12-13 08:05:20
 */
@RestController
@RequestMapping("/api/goodsmanager")
public class GoodsmanagerController {
    /**
     * 服务对象
     */
    @Resource
    private GoodsmanagerService goodsmanagerService;
    @Value("${filesavepath}")
    private String FILE_SAVE_PATH;
    @Value("${filesavefilepath}")
    private String FILE_SAVE_FILE_PATH;

    //查询全部商品
    @GetMapping("/myfindAll")
    public PageInfo<Goodsmanager> myfindAll(@RequestParam int appid, @RequestParam int shopid, @RequestParam int page, @RequestParam int size, @RequestParam int goodsclassoneid, @RequestParam int goodsclasstwoid) {

        System.out.println("appid:"+appid);
        System.out.println("shopid:"+shopid);
        System.out.println("page:"+page);
        System.out.println("size:"+size);

        return this.goodsmanagerService.myfindAll(appid,shopid,page,size,goodsclassoneid,goodsclasstwoid);
    }


    /**
     * 分页查询
     *
     * @param limitint 前n条
     */
    @GetMapping("/getgoodsbyinventory")
    public ResponseEntity<List<Goodsmanager>> getgoodsbyinventory(@RequestParam int appid,@RequestParam int shopid, @RequestParam(defaultValue = "10") int limitint) {

        return ResponseEntity.ok(this.goodsmanagerService.getgoodsbyinventory(appid,shopid,limitint));
    }

    /**
     * 分页查询
     *
     * @param goodsmanager 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Goodsmanager>> queryByPage(@RequestParam int appid,@RequestParam int shopid,@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {

        PageRequest pageRequest=PageRequest.of(page, size);
        Goodsmanager goodsmanager = new Goodsmanager();
        goodsmanager.setAppid(appid);
        goodsmanager.setShopid(shopid);
        return ResponseEntity.ok(this.goodsmanagerService.queryByPage(goodsmanager, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Goodsmanager> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.goodsmanagerService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param goodsmanager 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Goodsmanager> add( @RequestBody Goodsmanager goodsmanager) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(goodsmanager);
        System.out.println("=================="+jsonString);
        return ResponseEntity.ok(this.goodsmanagerService.insert(goodsmanager));
    }

    /**
     * 编辑数据
     *
     * @param goodsmanager 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Goodsmanager> edit(@RequestBody Goodsmanager goodsmanager) {
        System.out.println("id==========:"+goodsmanager.getId());
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String jsonString = objectMapper.writeValueAsString(goodsmanager);
            System.out.println("前台传入："+jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }




        return ResponseEntity.ok(this.goodsmanagerService.update(goodsmanager));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(@RequestParam Integer id) {
        return ResponseEntity.ok(this.goodsmanagerService.deleteById(id));
    }

}

